<?php

namespace App\Services;

use Illuminate\Support\Facades\Redis;

class OrderService
{
    private const ORDER_CANCEL_COUNT_PREFIX = 'order_cancel_count:';
    private const RESALE_ORDER_CANCEL_COUNT_PREFIX = 'resale_order_cancel_count:';

    public function increaseCancelCount($userId, $isResale)
    {
        if ($isResale) {
            $key = self::RESALE_ORDER_CANCEL_COUNT_PREFIX . $userId;
        } else {
            $key = self::ORDER_CANCEL_COUNT_PREFIX . $userId;
        }
        Redis::incr($key);
        Redis::expire($key, now()->secondsUntilEndOfDay());
    }

    public function getUserCancelCount($userId)
    {
        $res = Redis::get(self::ORDER_CANCEL_COUNT_PREFIX . $userId);
        if (!$res) {
            return 0;
        }

        return $res;
    }

    public function getUserResaleCancelCount($userId)
    {
        $res = Redis::get(self::RESALE_ORDER_CANCEL_COUNT_PREFIX . $userId, 0);
        if (!$res) {
            return 0;
        }

        return $res;
    }
}
